//
//  BindPhoneView.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/22.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

// MARK:- 设置代理
protocol BindPhoneDelegate {
    //接收绑定成功
    func bindSuccess()
    
    //接收绑定成功
    func bindSecreSuccess()
    
    //返回
    func back()
}

class BindPhoneView: UIView {
    
    //设置代理
    var delegate : BindPhoneDelegate?
    
    
    //手机号码
    var tfNum: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 0, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        //        lab.placeholder = "请输入您的手机号码"
        lab.plStrSize(str: "请输入您的手机号码", holderColor: UIColor.gray, textFontSize: 13)
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    
    //分割线
    lazy var line : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 45, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    
    //验证码
    var tfAuth: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 45, width: UIScreen.main.bounds.size.width - 100 - 15 - 50 , height: 45)
        lab.plStrSize(str: "请输入您的验证码", holderColor: UIColor.gray, textFontSize: 13)
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    //密码
    var tfPass: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 90, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        lab.plStrSize(str: "请输入您的密码", holderColor: UIColor.gray, textFontSize: 13)
        lab.setPass(setSecure: true, maxInput: 16)
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    //密码前置图片
    lazy var tfPassImg: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 97.5, width: 30, height: 30)
        
//        img.backgroundColor = UIColor.gray
        img.contentMode = UIViewContentMode.scaleAspectFit
        return img
    }()
    
    //分割线
    lazy var tfPassLine : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 135, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    
    
    //验证码按钮
    lazy var yzmBtn : CountDownBtn = {
        let btn = CountDownBtn()
        btn.backgroundColor = UIColor.orange
//        btn.initwith(frame: CGRect(x: SW - 15 - 100, y: 52.5, width: 100, height: 30), color: UIColor.orange, title: "获取验证码", superView: self, delegate: self)
        btn.frame = CGRect(x: SW - 15 - 100, y: 52.5, width: 100, height: 30)
        btn.backgroundColor = UIColor.orange
        btn.layer.cornerRadius = 8
        btn.setTitle("获取验证码", for: .normal)
        btn.addTarget(self, action: #selector(autoSEL(sender:)), for: .touchUpInside)
        
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        return btn
    }()
    
    //分割线
    lazy var newPassLine : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 90, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    

    //手机号前置图片
    lazy var tfOldImg: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 7.5, width: 30, height: 30)
        
//        img.backgroundColor = UIColor.gray
        img.contentMode = UIViewContentMode.scaleAspectFit
        return img
    }()
    
    //验证码前置图片
    lazy var tfNewImg: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 52.5, width: 30, height: 30)
        
//        img.backgroundColor = UIColor.gray
        img.contentMode = UIViewContentMode.scaleAspectFit
        return img
    }()

    
    //保存按钮
    lazy var saveBtn : UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.orange
        btn.frame = CGRect(x: 7.5, y: 110, width: UIScreen.main.bounds.width - 15, height: 40)
        btn.backgroundColor = UIColor.orange
        btn.layer.cornerRadius = 8
        btn.setTitle("保存", for: .normal)
         
        btn.addTarget(self, action: #selector(BindPhoneView.confirmSEL), for: .touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        return btn
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.setUI()
    }
    
}

extension BindPhoneView {
    func setUI() -> Void {
        
        tfOldImg.image = UIImage.init(named: "perInfo")
        tfNewImg.image = UIImage.init(named: "yzm")
        
        //手机号
        addSubview(tfNum)
        addSubview(tfOldImg)
        addSubview(line)
        
        //验证码
        addSubview(tfAuth)
        addSubview(tfNewImg)
        addSubview(newPassLine)
        addSubview(yzmBtn)
    
        //密码
//        addSubview(tfPass)
//        addSubview(tfPassImg)
//        addSubview(tfPassLine)
        
        addSubview(saveBtn)
    }
}


// MARK:- 单击事件
extension BindPhoneView {
    //验证码事件
    func autoSEL(sender : CountDownBtn) -> Void {
        SendAutoModel.shared.sendForgetAuto(sendType: 3, tfNum: tfNum) { (result) in
            print(result)
            
            self.endEditing(true)
            if result == "true" {
                sender.initwith(color: UIColor.orange, title: "发送验证码", superView: self)
            }
        }
    }
    
    //确定事件
    func confirmSEL() -> Void {
        print("绑定手机号")
        

        BindPhoneModel.shared.BindPhoneSEL(tfAuto: tfAuth, tfNum: tfNum) { (result) in
            print(result)

            if result == "绑定成功" {
        
                //存入本地，方便没有
                localSave.set(self.tfNum.text, forKey: "bindPhone")
                localSave.synchronize()

        
                //改变单例，下次进入不可进入绑定手机号，防止误操作
                PersonInfoModel.shared.bindPhone = "true"
                
                self.delegate?.bindSuccess()
        
            }
        }
    }
}


